package com.telecom.cms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dto.*;

public class LOC_LATNDAO {
	private static final String SQL_FINDBYID = "select * from "
			+ Constant.LOC_LATN + " where latn_Id = ? ";

	private static final String SQL_FINDALL = "select * from "
			+ Constant.LOC_LATN + " order by latn_Id";

	private Connection conn = null;

	public LOC_LATNDAO() {
		this.conn =	GetConnect.getConnection();;
	}

	public LOC_LATNDTO findById(String latn_id) {
		PreparedStatement ps = null;
		ResultSet rs = null;
		LOC_LATNDTO dto = new LOC_LATNDTO();
		try {
			ps = conn.prepareStatement(SQL_FINDBYID);
			ps.setInt(1, Integer.parseInt(latn_id));

			rs = ps.executeQuery();
			if (rs.next()) {

				dto.setLatn_id(latn_id);
				dto.setLatn_name(rs.getString("Latn_Name"));
				dto.setLatn_cd(rs.getString("Latn_Cd"));
				dto.setLatn_desc(rs.getString("Latn_Desc"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return dto;
	}

	public List findAll() {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(SQL_FINDALL);
			rs = ps.executeQuery();
			while (rs.next()) {
				LOC_LATNDTO dto = new LOC_LATNDTO();
				dto.setLatn_id((new Integer (rs.getInt("Latn_id"))).toString());
				dto.setLatn_name(rs.getString("Latn_Name"));
				dto.setLatn_cd(rs.getString("Latn_Cd"));
				dto.setLatn_desc(rs.getString("Latn_Desc"));
				list.add(dto);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
